<template>
  <div class="userList-container">
    <el-form
      :model="formData"
      inline
      class="myForm"
    >
      <el-form-item prop="username">
        <el-input
          v-model="formData.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          v-model="formData.mobile"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search" type="primary" icon="el-icon-search"
          >查找</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="tableLoading" :data="tableData" border class="myTab">
      <el-table-column
        align="center"
        label="用户名id"
        prop="id"
      ></el-table-column>
      <el-table-column
        align="center"
        label="用户名"
        prop="username"
      ></el-table-column>
      <el-table-column
        align="center"
        label="手机号码"
        prop="mobile"
      ></el-table-column>
      <el-table-column align="center" label="性别" prop="gender">
        <template slot-scope="scope">
          <el-tag>{{
            scope.row["gender"] | tableFilter(genderOptions)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="生日"
        prop="birthday"
      ></el-table-column>
      <el-table-column align="center" label="用户等级" prop="userLevel">
        <template slot-scope="scope">
          <el-tag>{{
            scope.row["userLevel"] | tableFilter(userLevelOptions)
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{
            scope.row["status"] | tableFilter(accountStatus)
          }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="myStyle"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageInfo.limit"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>
  </div>
</template>

<script>
// 请求列表的url http://192.168.200.251:8888/admin/user/list

import axios from "axios";

export default {
  name: "userList",
  componentTitle: "用户列表",
  data() {
    return {
      tableLoading: false,
      pageInfo: {
        page: 1,
        limit: 10,
        total: 0,
      },
      formData: {
        username: "",
        mobile: "",
      },
      tableData: [],
      genderOptions: [
        { label: "未知", value: "0" },
        { label: "男", value: "1" },
        { label: "女", value: "2" },
      ],
      userLevelOptions: [
        { label: "普通用户", value: "0" },
        { label: "VIP用户", value: "1" },
        { label: "高级VIP用户", value: "2" },
      ],
      accountStatus: [
        { label: "可用", value: "0" },
        { label: "禁用", value: "1" },
        { label: "注销", value: "2" },
      ],
    };
  },
  mounted() {
    this.search();
  },
  filters: {
    tableFilter(value, options) {
      let index = options.findIndex((item) => {
        return Number(item.value) === Number(value);
      });

      if (index < 0) {
        return "---";
      } else {
        return options[index].label;
      }
    },
  },
  methods: {
    search() {
      this.tableLoading = true;

      axios({
        url: "http://192.168.200.251:8888/admin/user/list",
        method: "get",
        params: {
          page: this.pageInfo.page,
          limit: this.pageInfo.limit,
          ...this.formData,
        },
        headers: {
          "Shopping-Admin-Token": sessionStorage.getItem(
            "Shopping-Admin-Token"
          ),
        },
      }).then((res) => {
        this.tableLoading = false;
        this.tableData = res.data.data.list;

        this.pageInfo.total = res.data.data.total;
        console.log(res);
      });
    },
    // 每页多少条切换
    handleSizeChange(value) {
      this.pageInfo.limit = value;

      this.search();
    },
    // 当前页码的切换
    handleCurrentChange(value) {
      this.pageInfo.page = value;

      this.search();
    },
  },
};
</script>

<style lang="scss" scoped>
.myStyle{
  margin-top: 70px;
  margin-left: 80px;
}

.myForm{
display: flex;
justify-content: flex-end; 
margin-top: 10px;
margin-right: 22px;
}

.myTab{
  width: 95%;
  text-align: center;
  margin: 0 auto;

}


</style>

